﻿using System;
using System.Runtime.Serialization;
using System.Windows.Media;
using System.Windows.Shapes;
using System.Xml.Linq;

namespace XamlaApp.ViewModels
{
    [DataContract]
    public class ShapeViewModel : FrameworkElementViewModel
    {
        private SolidColorBrush _fillBrush = new SolidColorBrush(Colors.Blue);
        [IgnoreDataMember]
        public SolidColorBrush FillBrush
        {
            get { return _fillBrush; }
            set { _fillBrush = value; 
            PropertyChanging("FillBrush");}
        }

        private SolidColorBrush _strokeBrush = new SolidColorBrush(Colors.Blue);
        [IgnoreDataMember]
        public SolidColorBrush StrokeBrush
        {
            get { return _strokeBrush; }
            set { _strokeBrush = value;
            PropertyChanging("StrokeBrush");
            }
        }

        private double _strokeThickness = 0;
        [DataMember]
        public double StrokeThickness
        {
            get { return _strokeThickness; }
            set { _strokeThickness = Math.Round(value); 
            PropertyChanging("StrokeThickness");}
        }

        internal override System.Xml.Linq.XElement GenerateXaml(System.Xml.Linq.XElement element)
        {
            element.Add(
                new XAttribute("StrokeThickness",StrokeThickness),
                new XAttribute("Stroke",StrokeBrush.Color.ToString()),
                new XAttribute("Fill",FillBrush.Color.ToString())
                );
            return base.GenerateXaml(element);
        }


        #region Json Proxy properties
        // These properties provide a serializable link to other view model properties that cannot be serialized inherently.
        // Sort of like value converters... these are not for databinding

        [DataMember]
        public Color FillBrushProxy
        {
            get { return FillBrush.Color; }
            set
            {
                if (FillBrush == null) FillBrush = new SolidColorBrush();
                FillBrush.Color = value;
            }
        }

        [DataMember]
        public Color StrokeBrushProxy
        {
            get { return StrokeBrush.Color; }
            set
            {
                if (StrokeBrush == null) StrokeBrush = new SolidColorBrush();
                StrokeBrush.Color = value;
            }
        }
        #endregion
    }
}
